You can play a sound stored in a resource by calling the SndPlay function, which requires a handle to an existing 'snd ' resource. An 'snd ' resource contains sound commands that play the desired sound. The 'snd ' resource might also contain sound data. If it does (as in the case of a sampled sound), that data might be either compressed or noncompressed. SndPlay decompresses the data, if necessary, to play the sound. Listing 0-1 illustrates how to play a sound resource.
Listing 1 Playing a sound resource with SndPlay
FUNCTION MyPlaySndResource (mySndID: Integer): OSErr;
CONST
kAsync = TRUE; {for asynchronous play}
VAR
mySndHandle: Handle; {handle to an 'snd ' resource}
myErr: OSErr;
BEGIN
mySndHandle := GetResource('snd ', mySndID);
myErr := ResError; {remember any error}
IF mySndHandle <> NIL THEN {check for a NIL handle}
BEGIN
HLock(mySndHandle); {lock the sound data}
myErr := SndPlay(NIL, mySndHandle, NOT kAsync);
HUnlock(mySndHandle); {unlock the sound data}
ReleaseResource(mySndHandle);
END;
MyPlaySndResource := myErr; {return the result}
END;
When you pass SndPlay a NIL sound channel pointer in its first parameter, the Sound Manager automatically allocates a sound channel (in the application's heap) and then disposes of it when the sound has completed playing. Note, however, that when your application does pass NIL as the pointer to a sound channel, the third parameter to SndPlay is ignored; the sound plays synchronously even if you specify that you want it to play asynchronously.
The handle you pass to SndPlay must be locked for as long as the sound is playing.
| Previous | Chapter contents | Chapter top | Section top | Next |